package com.gxuwz.business.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.gxuwz.business.entity.Fee;
import com.gxuwz.business.entity.StandFee;
import com.gxuwz.business.entity.Standard;
import com.gxuwz.business.entity.Student;
import com.gxuwz.business.entity.SysUser;
import com.gxuwz.business.service.IFeeService;
import com.gxuwz.business.service.IStandFeeService;
import com.gxuwz.business.service.IStandardService;
import com.gxuwz.business.service.IStudentService;
import com.gxuwz.business.service.ISysUserService;
import com.gxuwz.util.Pager;
import com.gxuwz.util.TimeSplitUtils;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class StandFeeAction extends BaseAction implements Preparable,ModelDriven<StandFee>{

	private static final long serialVersionUID = 3084333006705377214L;
	
	private static final String STANDFEE_LIST = "/WEB-INF/page/standFee/standFee_list.jsp";
	private static final String STANDFEE_ADD = "/WEB-INF/page/standFee/standFee_add.jsp";
	private static final String STANDFEE_CHOICE = "/WEB-INF/page/standFee/standFee_add.jsp";
	
	private SysUser user;

	private StandFee standFee;
	
	private Student student;
	
	/**
	 * 获取的standId和count;
	 */
	private String standFeeDesc;
	
	private Pager<StandFee> standFeeList;
	
	private String[]standFeeCount;
	
	private String[]standFeeCounts;
	
	private List<Student> studentList;
	
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private IStudentService studentService;
	@Autowired
	private IStandFeeService standFeeService;
	@Autowired
	private IStandardService standardService;
	@Autowired
	private IFeeService feeService;
	
	

	/**
	 * 打开费用标准单界面
	 * @return
	 */
	public String standFeeList(){
		forwardView = STANDFEE_LIST;
		return SUCCESS;
	}
	
	/**
	 * 分页查询
	 */
	public String getByPager(){
		//时间段查询
				if(null != timeSection && !"".equals(timeSection)){
					dateStringList = TimeSplitUtils.timeSplitToString(timeSection);
					String openTime = dateStringList.get(0);
					String endTime = dateStringList.get(1);
					student.setOpenTime(openTime);
					student.setEndTime(endTime);
//					System.out.println(student.getOpenTime()+" "+student.getEndTime());
				}
				standFeeList = standFeeService.getByPager(standFee, page, limit);
				JsonData = JSONObject.fromObject(standFeeList);
				return "JsonObject";
	}
	
	/**
	 * 打开增加治疗标准费用页面
	 */
	public String openAdd(){
		user = (SysUser)getSession().getAttribute("user");
		studentList = studentService.getAll();
		forwardView = STANDFEE_ADD;
		return SUCCESS;
	}
	
	/**
	 * 返回挂号集合
	 */
	public String getAllStu(){
		studentList = studentService.getAllSucceed();
		jsonArray = JSONArray.fromObject(studentList);
		return "JsonArray";
	}
	
	/**
	 * 打开治疗标准费用选中弹出、窗
	 */
	public String openChoice(){
		forwardView = STANDFEE_CHOICE;
		return SUCCESS;
	}
	
	public String add(){
		user = (SysUser)getSession().getAttribute("user");
		Student s = new Student();s.setRegId(standFee.getRegId());
		Fee fee = feeService.findByRegId(standFee.getRegId()); 
		s = studentService.findById(s);
		//分割成'药品编号a数量'的格式
		standFeeCount = standFeeDesc.split("b");
		for(String stand: standFeeCount){
			//继续分割成 ['药品','数量']的数组格式
			standFeeCounts = stand.split("a");
			//通过药品编号查询价格
			standFee.setStandId(standFeeCounts[0]);
			Standard standard = new Standard();
			standard.setStandId(standFeeCounts[0]);
		    standard = standardService.findById(standard);
		    standFee.setCount(Integer.valueOf(standFeeCounts[1]));
		    standFee.setTotalFee(standard.getStandPrice()*Integer.valueOf(standFeeCounts[1]));
		    standFee.setStandName(standard.getStandName());
		    standFee.setUserId(user.getUserId());
		    standFee.setUserName(user.getUserName());
		    standFee.setStuName(s.getStuName());
			isSucceed = standFeeService.add(standFee);
			//更新缴费信息
			fee.setStandFee(fee.getStandFee()+standard.getStandPrice()*Integer.valueOf(standFeeCounts[1]));
			fee.setFeeTotal(fee.getFeeTotal()+standard.getStandPrice()*Integer.valueOf(standFeeCounts[1]));
			fee.setFeeNeed(fee.getFeeNeed()+standard.getStandPrice()*Integer.valueOf(standFeeCounts[1]));
			//主要用来更改溢出状态
			if(fee.getFeeNeed() > 0){
				fee.setStatus(0);
			}
			feeService.update(fee);
			if(!isSucceed){
				System.err.println("添加治疗费用发生错误");
			}
		}
		jsonString = jsonToString(isSucceed);
		return "JsonString";
	}
	
	@Override
	public StandFee getModel() {
		return standFee;
	}

	@Override
	public void prepare() throws Exception {
		if(null == standFee){
			standFee = new StandFee();
		}
	}

	public String getStandFeeDesc() {
		return standFeeDesc;
	}

	public void setStandFeeDesc(String standFeeDesc) {
		this.standFeeDesc = standFeeDesc;
	}

	public String[] getStandFeeCount() {
		return standFeeCount;
	}

	public void setStandFeeCount(String[] standFeeCount) {
		this.standFeeCount = standFeeCount;
	}

	public String[] getStandFeeCounts() {
		return standFeeCounts;
	}

	public void setStandFeeCounts(String[] standFeeCounts) {
		this.standFeeCounts = standFeeCounts;
	}

	public List<Student> getStudentList() {
		return studentList;
	}

	public void setStudentList(List<Student> studentList) {
		this.studentList = studentList;
	}

	
}
